package data

import (
	"sync"
)

// DC 数据中心单例，管理在线设备
type DC struct {
	devices []*Device
	mu      sync.Mutex
}

var (
	instance *DC
	once     sync.Once
)

// GetInstance 获取DC单例
func GetInstance() *DC {
	once.Do(func() {
		instance = &DC{
			devices: make([]*Device, 0),
		}
	})
	return instance
}

// AddDevice 添加设备到列表
func (dc *DC) AddDevice(dev *Device) {
	dc.mu.Lock()
	defer dc.mu.Unlock()
	dc.devices = append(dc.devices, dev)
}

// GetDevices 获取当前在线设备列表
func (dc *DC) GetDevices() []*Device {
	dc.mu.Lock()
	defer dc.mu.Unlock()
	return dc.devices
}

// ClearDevices 清空设备列表
func (dc *DC) ClearDevices() {
	dc.mu.Lock()
	defer dc.mu.Unlock()
	dc.devices = make([]*Device, 0)
}
